package com.yswy.backstage.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yswy.backstage.service.ISysConfigService;
import com.yswy.backstage.service.ZfbPayKeyService;
import com.yswy.common.utils.dingding.DingUtil;
import com.yswy.common.utils.redis.RedisUtil;
import com.yswy.common.zfbOrder.ZfbKeyVo;
import com.yswy.domain.entity.ZfbPayKey;
import com.yswy.domain.mapper.ZfbPayKeyMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ZfbPayKeyServiceImpl extends ServiceImpl<ZfbPayKeyMapper, ZfbPayKey>
        implements ZfbPayKeyService {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private DingUtil dingUtil;
//    @Autowired
//    private PayOrderService payOrderService;
    //    //支付宝定时执行周期 10min
//    public static Long period = 4*60*1000L;
//    //上一次支付成功率
    private Double lastPaySuccessRate = 0d;
    //    public static Double compareRate = 0.4;
    @Autowired
    private ISysConfigService configService ;

    /**
     * 按滑动窗口检查支付成功率，进行支付宝切换
     */
    @Override
    public void checkAliPay(String payMethod) {
//        Long period = new Long(configService.selectConfigByKey("checkAliPay_period"));
//        new Timer().schedule(new TimerTask() {
//            @Override
//            public void run() {
//                try {
//                    Double compareRate = new Double(configService.selectConfigByKey("checkAliPay_compareRate"));
//                    //查询]支付宝成功概率
//                    ZfbPayKey zfbPayKey =  (ZfbPayKey) redisUtil
//                            .getObejct(payMethod);
//                    if (zfbPayKey == null){
//                        return;
//                    }
//                    int minutes = (int) (period / 60 / 1000);
//                    //查出周期内所有订单
//                    int lastAllCount = payOrderService.count(new LambdaQueryWrapper<PayOrder>()
//                            .ge(PayOrder::getCreateTime, new LocalDateTime().minusMinutes(minutes).toString("yyyy-MM-dd HH:mm:ss"))
//                            .eq(PayOrder::getAppId ,zfbPayKey.getAppId() )
//                            .in(PayOrder::getOrderType , 0,1,2 ,4)
//                            .eq(PayOrder::getPayTerrace , 0));
//                    //查出周期内所有成功支付订单
//                    int lastSuccessCount = payOrderService.count(new LambdaQueryWrapper<PayOrder>()
//                            .ge(PayOrder::getCreateTime, new LocalDateTime().minusMinutes(minutes).toString("yyyy-MM-dd HH:mm:ss"))
//                            .eq(PayOrder::getOrderStatus, 2)
//                            .in(PayOrder::getOrderType , 0,1,2 ,4)
//                            .eq(PayOrder::getAppId ,zfbPayKey.getAppId() )
//                            .eq(PayOrder::getPayTerrace , 0));
//                    double currentPaySuccessRate = 0 ;
//                    if (lastSuccessCount > 0){
//
//                        currentPaySuccessRate = (double)lastSuccessCount / (double)lastAllCount;
//                    }
//                    //计算周期内支付成功率
//                    log.info("开始支付宝检测"+ "当前周期成功率:" + currentPaySuccessRate
//                            +"   上一次周期成功概率:" + lastPaySuccessRate
//                            +"   周期内所有订单数:" + lastAllCount
//                            +"   查出周期内所有成功支付订单数:" + lastSuccessCount);
//                    if (lastPaySuccessRate != 0) {
//                        //如果当前支付成功率低于上一轮的20%
//                        if (currentPaySuccessRate <= lastPaySuccessRate *compareRate) {
//                            //进行支付宝切换
////                            redisUtil.delete(payMethod);
//                            //进行邮箱通知
//                            try {
//                                dingUtil.sendMsg("支付宝预警公告：\n支付宝账号"+zfbPayKey.getEMail()+
//                                        "异常("+period/60/1000+"分钟支付率为"+currentPaySuccessRate +")"+
//                                        "异常("+period/60/1000+"分钟支付率为"+currentPaySuccessRate +")"+
//                                        "，检测时间"+
//                                        DateUtils.getTime() +"，请管理员及时登陆处理。");
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                            }
//                        }
//                    }
//                    lastPaySuccessRate = currentPaySuccessRate;
//                }catch (Exception e){
//                    e.printStackTrace();
//                }
//            }
//        }, 1000, period);
    }


    @Override
    public ZfbKeyVo getZfbKeyVo(Long id ,ZfbPayKey zfbPayKey ) {
        if (zfbPayKey == null){
            zfbPayKey = getById(id);
        }
        ZfbKeyVo zfbKeyVo = new ZfbKeyVo();
        zfbKeyVo.setId(zfbPayKey.getId());
        zfbKeyVo.setZfbKey(zfbPayKey.getZfbKey());
        zfbKeyVo.setAppId(zfbPayKey.getAppId());
        zfbKeyVo.setJavaKey(zfbPayKey.getJavaKey());
        zfbKeyVo.setCertPath(zfbPayKey.getCertPath());
        zfbKeyVo.setRootCertPath(zfbPayKey.getRootCertPath());
        zfbKeyVo.setPublicCertPath(zfbPayKey.getPublicCertPath());
        zfbKeyVo.setSingType(zfbPayKey.getSingType());
        return zfbKeyVo;
    }




}
